package com.huaxin.hxmodulemqtt.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxin.feignclient.device.DeviceClient;
import com.huaxin.feignclient.device.models.DeviceDataCollectionWideAddDTO;
import com.huaxin.hxmodulemqtt.dto.AecDataReportReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class DataParsingServiceImpl {
    private final DeviceClient deviceClient;
    private final DataProcessor dataProcessor;

    public void parseData(String topic, String message) throws JsonProcessingException {
        log.info("✅ MQTT 订阅数据 | {} | {}", topic, message);
        ObjectMapper objectMapper = new ObjectMapper();
        AecDataReportReqDTO dto = objectMapper.readValue(message, AecDataReportReqDTO.class);
        List<DeviceDataCollectionWideAddDTO> deviceDataCollectionWideAddDTOS = dataProcessor.processBatch(dto);
        deviceClient.addDeviceData(deviceDataCollectionWideAddDTOS, dto.getIstation());
    }
}
